﻿using System;
using System.ComponentModel.Design;
using System.Text;
using System.Web.UI.Design;
using System.Drawing;
using System.Web.UI;

namespace TPG.SharePoint.Workflow.Controls.Design
{
    [SupportsPreviewControl(true)]
    public class InputFormDesigner
        : ControlDesigner
    {
        bool _showContainerTable = true;

        public bool ShowContainerTable 
        {
            get { return _showContainerTable; }
            set
            {
                _showContainerTable = value;
                UpdateDesignTimeHtml();
            }
        }

        public override DesignerActionListCollection ActionLists
        {
            get
            {
                DesignerActionListCollection lists = base.ActionLists;
                lists.Add(new InputFormDesignerActionList(this));
                return lists;
            }
        }

        protected InputForm Form
        {
            get { return (InputForm)Component; }
        }

        protected override bool UsePreviewControl
        {
            get { return true; }
        }

        public override string GetDesignTimeHtml(DesignerRegionCollection regions)
        {
            EditableDesignerRegion region = new EditableDesignerRegion(
                this, "ContentRegion");
            region.EnsureSize = true;
            regions.Add(region);
            return GetDesignTimeHtml();
        }

        public override string GetDesignTimeHtml()
        {
            InputForm form = (InputForm)ViewControl;
            form.Controls.Clear();
            SPDesignTimeStyleManager.ApplySharePointStyle(form);
            form.Attributes.Add(DesignerRegion.DesignerRegionAttributeName, "0");
            if (ShowContainerTable)
            {
                string viewHtml = @"
                <table cellspacing=""0"" cellpadding=""0"">
                    <tr>
                        <td nowrap valign=""top"" align=""middle"" style=""font-family:tahoma;color:button-text;background-color:{0}"">
                            InputForm
                        </td>
                    </tr>
                    <tr>
                        <td nowrap style=""vertical-align:top;"">
                            {1}
                        </td>
                    </tr>
                </table>";
                return String.Format(
                    viewHtml,
                    ColorTranslator.ToHtml(SystemColors.Control),
                    base.GetDesignTimeHtml());
            }
            else
            {
                return base.GetDesignTimeHtml();
            }
        }

        public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            InputForm form = Form;
            StringBuilder content = new StringBuilder();
            foreach (Control control in form.Controls)
            {
                content.Append(ControlPersister.PersistControl(control, host));
            }
            return content.ToString();
        }

        public override void SetEditableDesignerRegionContent(
            EditableDesignerRegion region, string content)
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            InputForm form = Form;
            form.Controls.Clear();
            if (String.IsNullOrEmpty(content) == false)
            {
                foreach (Control control in ControlParser.ParseControls(host, content))
                {
                    FormSection section = control as FormSection;
                    if (section != null)
                    {
                        form.Controls.Add(section);
                    }
                }
            }
            ViewControlCreated = false;
        }

        class InputFormDesignerActionList
            : DesignerActionList
        {
            InputFormDesigner _designer;

            public bool ShowContainerTable 
            {
                get { return _designer.ShowContainerTable; }
                set { _designer.ShowContainerTable = value; }
            }

            public InputFormDesignerActionList(InputFormDesigner designer)
                : base(designer.Component)
            {
                _designer = designer;
            }

            public override DesignerActionItemCollection GetSortedActionItems()
            {
                DesignerActionItemCollection actions = new DesignerActionItemCollection();
                actions.Add(new DesignerActionPropertyItem(
                    "ShowContainerTable", "Show Design-Time Container"));
#if DEBUG
                actions.Add(new DesignerActionMethodItem(
                    this, "GetHtml", "Get Html"));
#endif
                return actions;
            }
#if DEBUG
            public void GetHtml()
            {
                HtmlPreviewer.ShowForDesigner(_designer);
            }
#endif
        }
    }
}
